﻿using AutoMapper;
using Common.Damin;
using Common.Infrastructrue.Interface;
using Common.Service;
using MediatR;
using RabitPart.WritedPartApi.Application.Comman.CultivationFile;

namespace RabitPart.WritedPartApi.Application.CommanHandel.CultivationFile
{
    public class CultivationUpdateCommanHandel : IRequestHandler<CultivationUpdateComman, ApiResult>
    {
        private readonly ICultivationResponstity cultiRE;
        private readonly IMapper mapper;
        private readonly ILogger<CultivationUpdateCommanHandel> logger;
        public CultivationUpdateCommanHandel(ICultivationResponstity cultiRE, IMapper mapper, ILogger<CultivationUpdateCommanHandel> logger)
        {
            this.cultiRE = cultiRE;
            this.mapper = mapper;
            this.logger = logger;
        }
        public Task<ApiResult> Handle(CultivationUpdateComman request, CancellationToken cancellationToken)
        {
            try
            {
                var cultivation = mapper.Map<Cultivation>(request);
                cultiRE.UpdateAsync(cultivation);

                return Task.FromResult(ApiResult.Success(StatusCodeEnum.Ok));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "CultivationCommanHandel");
                return Task.FromResult(ApiResult.Fail(ex.Message, StatusCodeEnum.Fail));
            }
        }
    }
}
